package de.graffiti.paint;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.io.IOException;

import javax.swing.ButtonGroup;
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JToggleButton;
import java.awt.FlowLayout;

@SuppressWarnings("serial")
public class PaintExample extends JApplet
{

	private JPanel jContentPane = null;
	private JPanel jPanelSitebar = null;
	private JPaintPanel jPaintPanel = null;
	private JButton jSaveButton = null;
	private JComboBox jComboBox = null;
	private JPanel jPanel = null;
	private ImageInfo image; // @jve:decl-index=0:
	@SuppressWarnings("unused")
	private JScrollPane scrollPane;
	private ButtonGroup colorGroup = new ButtonGroup();
	JPanel colorPanel;
	private JScrollPane jScrollPane = null;
	/**
	 * This is the xxx default constructor
	 */
	public PaintExample()
	{
		super();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	public void init()
	{
		this.setSize(900, 900);
		int wallpaperid = 0;
		int contentid = 0;
		int contentwidth = 0;
		int contentheight = 0;
		String sessionid = null;
		boolean repeating = false;

		try
		{
			wallpaperid = Integer.parseInt(getParameter("wallpaperid"));
			contentid = Integer.parseInt(getParameter("contentid"));
			contentwidth = Integer.parseInt(getParameter("contentwidth"));
			contentheight = Integer.parseInt(getParameter("contentheight"));
		} catch (NumberFormatException e)
		{

		}
		sessionid = getParameter("sessionid");
		if (getParameter("repeating").equals("true"))
		{
			repeating = true;
		}

		image = new ImageInfo(wallpaperid, contentid, contentwidth, contentheight, sessionid, repeating);
		try
		{
			image.loadImage();
			this.setContentPane(getJContentPane());
		} catch (IOException e)
		{
			System.out.println("Wallpaper " + image.getWallpaperid() + " nicht gefunden");
		}

	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane()
	{
		if (jContentPane == null)
		{
			jContentPane = new JPanel();
			jContentPane.setLayout(new BorderLayout());
			jContentPane.add(getJScrollPane(), BorderLayout.CENTER);
			jContentPane.add(getJPanelSitebar(), BorderLayout.WEST);

		}
		return jContentPane;
	}

	/**
	 * This method initializes jPanelSitebar
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJColorPanel()
	{
		if (colorPanel == null)
		{
			colorPanel = new JPanel();
			@SuppressWarnings("unused")
			GridBagLayout gbl = new GridBagLayout();
			GridLayout colorLayout = new GridLayout(0, 3, 0, 0);
			colorPanel.setLayout(colorLayout);

			colorPanel.add(getColorButton(Color.black), null);
			colorPanel.add(getColorButton(Color.gray), null);
			colorPanel.add(getColorButton(Color.lightGray), null);
			colorPanel.add(getColorButton(Color.red), null);
			colorPanel.add(getColorButton(Color.magenta), null);
			colorPanel.add(getColorButton(Color.blue), null);
			colorPanel.add(getColorButton(Color.cyan), null);
			colorPanel.add(getColorButton(Color.yellow), null);
			colorPanel.add(getColorButton(Color.orange), null);
			colorPanel.add(getColorButton(Color.pink), null);
			colorPanel.add(getColorButton(Color.green), null);
			colorPanel.add(getColorButton(Color.white), null);

		}
		return colorPanel;
	}

	/**
	 * This method initializes jPanelSitebar
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJPanelSitebar()
	{
		if (jPanelSitebar == null)
		{
			jPanelSitebar = new JPanel();
			jPanelSitebar.setLayout(new BorderLayout());
			jPanelSitebar.add(getJPanel(), BorderLayout.NORTH);
		}
		return jPanelSitebar;
	}

	/**
	 * This method initializes jPaintPanel
	 * 
	 * @return javax.swing.JPanel
	 */

	/**
	 * This method initializes jSaveButton
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getJSaveButton()
	{
		if (jSaveButton == null)
		{
			jSaveButton = new JButton();
			jSaveButton.addActionListener(new ActionListener()
			{

				public void actionPerformed(ActionEvent e)
				{

					// Output output = new ExportToFile();
					Output output = new ExportToWebservice(image);
					// Output output = new ExportToEJB(image);
					try
					{
						output.export(jPaintPanel.getSvgGenerator());
					} catch (IOException e1)
					{
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}

				}
			});
			jSaveButton.setText("Save");
		}
		return jSaveButton;
	}

	/**
	 * This method initializes jComboBox
	 * 
	 * @return javax.swing.JComboBox
	 */
	private JComboBox getJComboBox()
	{
		if (jComboBox == null)
		{
			jComboBox = new JComboBox();

			jComboBox.addItem(1);
			jComboBox.addItem(2);
			jComboBox.addItem(3);

			jComboBox.addItemListener(new ItemListener()
			{
				public void itemStateChanged(ItemEvent e)
				{
					JComboBox selectedChoice = (JComboBox) e.getSource();
					jPaintPanel.setStroke((selectedChoice.getSelectedIndex() + 1) * 3);
				}
			});

		}
		return jComboBox;
	}

	void add(int y, GridBagLayout gbl, Component c)
	{
		GridBagConstraints gbc = new GridBagConstraints();
		gbc.fill = GridBagConstraints.BOTH;
		gbc.gridx = 1;
		gbc.gridy = y;
		gbc.gridwidth = 1;
		gbc.gridheight = 1;
		gbc.weightx = 1;
		gbc.weighty = 1;
		gbl.setConstraints(c, gbc);
		jPanel.add(c);
	}

	private JToggleButton getColorButton(final Color color)
	{
		JToggleButton jColorButton = new JToggleButton();
		jColorButton.setBackground(color);
		jColorButton.setPreferredSize(new Dimension(20, 20));
		jColorButton.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				jPaintPanel.setColor(color);
			}
		});
		colorGroup.add(jColorButton);
		return jColorButton;
	}

	/**
	 * This method initializes jPanel
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJPanel()
	{
		if (jPanel == null)
		{
			GridBagLayout gbl = new GridBagLayout();

			jPanel = new JPanel();
			jPanel.setLayout(gbl);
			// jPanel.add(getJSaveButton(), null);
			// jPanel.add(getJComboBox(), null);
			add(1, gbl, getJComboBox());
			add(2, gbl, getJSaveButton());
			add(3, gbl, getJColorPanel());

		}
		return jPanel;
	}

	/**
	 * This method initializes jScrollPane
	 * 
	 * @return javax.swing.JScrollPane
	 */
	private JScrollPane getJScrollPane()
	{
		if (jScrollPane == null)
		{
			jScrollPane = new JScrollPane(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, // vertical
					// bar
					JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
			jScrollPane.setBorder(null);
			jScrollPane.setViewportView(getJPaintPanel());

		}
		return jScrollPane;
	}

	/**
	 * This method initializes jPanel1
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJPaintPanel()
	{
		if (jPaintPanel == null)
		{
			jPaintPanel = new JPaintPanel(image);
			jPaintPanel.setLayout(new FlowLayout());
			jPaintPanel.setPreferredSize(new Dimension(image.getHeight(), image.getWidth()));
			jPaintPanel.setMinimumSize(new Dimension(image.getHeight(), image.getWidth()));
			System.out.println(image.getHeight() + " " + image.getWidth());
		}
		return jPaintPanel;
	}

} // @jve:decl-index=0:visual-constraint="10,10"
